import advView from '$V/adv'
import{
    isLoginRequest,
    logoutRequest
} from '../api/admin'

export default async (req,res,next)=>{

    //路由切换的时候验证登录
  try {
    await isLoginRequest();
  } catch (e) {
    //请求异常后提示用户
    alert("有异常" + e.message)

    //判断是否是权限异常
    if (!e.isLogin) {
      return router.go("/login");
    }
  }

    next(advView({
        subRoute: res.subRoute(),
        url: req.url
    }))

    //获取元素
    const oLogout = document.getElementById('logout')
    oLogout.onclick = async function(){
        try{
            //响应后端 清除状态改为0
            await logoutRequest()
            //前台响应为清除token
            localStorage.removeItem('user_token')
            //接下来跳转页面
            router.go("/login")
        }catch(e){
            alert("退出登录异常" + e.message)
        }
    }

}